gtkcolorswatch: Stop using ::key-press-event
authorCarlos Garnacho <carlosg@gnome.org>
Mon, 5 Mar 2018 13:59:21 +0000 (14:59 +0100)
committerCarlos Garnacho <carlosg@gnome.org>
Thu, 5 Apr 2018 17:26:53 +0000 (19:26 +0200)
Use GtkEventControllerKey for the task

gtk/gtkcolorswatch.c

index 4872f4b667975f64bf6636f2178f9a2cb8917cdf..44a04c685b0a188db8a683822cbba2367e10882b 100644 (file)
@@ -39,6 +39,7 @@
 #include "gtksnapshot.h"
 #include "gtkstylecontextprivate.h"
 #include "gtkwidgetprivate.h"
+#include "gtkeventcontrollerkey.h"
 
 #include "a11y/gtkcolorswatchaccessibleprivate.h"
 
@@ -66,6 +67,7 @@ struct _GtkColorSwatchPrivate
 
   GtkGesture *long_press_gesture;
   GtkGesture *multipress_gesture;
+  GtkEventController *key_controller;
   GtkWidget *overlay_widget;
 
   GtkWidget *popover;
@@ -249,14 +251,13 @@ swatch_drag_data_received (GtkWidget        *widget,
 }
 
 static gboolean
-swatch_key_press (GtkWidget   *widget,
-                  GdkEventKey *event)
+key_controller_key_pressed (GtkEventControllerKey *controller,
+                            guint                  keyval,
+                            guint                  keycode,
+                            GdkModifierType        state,
+                            GtkWidget             *widget)
 {
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
-  guint keyval;
-
-  if (gdk_event_get_keyval ((GdkEvent *) event, &keyval))
-    return GDK_EVENT_PROPAGATE;
 
   if (keyval == GDK_KEY_space ||
       keyval == GDK_KEY_Return ||
@@ -273,9 +274,6 @@ swatch_key_press (GtkWidget   *widget,
       return TRUE;
     }
 
-  if (GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->key_press_event (widget, event))
-    return TRUE;
-
   return FALSE;
 }
 
@@ -515,6 +513,7 @@ swatch_dispose (GObject *object)
 
   g_clear_object (&swatch->priv->long_press_gesture);
   g_clear_object (&swatch->priv->multipress_gesture);
+  g_clear_object (&swatch->priv->key_controller);
 
   G_OBJECT_CLASS (gtk_color_swatch_parent_class)->dispose (object);
 }
@@ -535,7 +534,6 @@ gtk_color_swatch_class_init (GtkColorSwatchClass *class)
   widget_class->drag_begin = swatch_drag_begin;
   widget_class->drag_data_get = swatch_drag_data_get;
   widget_class->drag_data_received = swatch_drag_data_received;
-  widget_class->key_press_event = swatch_key_press;
   widget_class->popup_menu = swatch_popup_menu;
   widget_class->size_allocate = swatch_size_allocate;
   widget_class->state_flags_changed = swatch_state_flags_changed;
@@ -590,6 +588,10 @@ gtk_color_swatch_init (GtkColorSwatch *swatch)
   g_signal_connect (swatch->priv->multipress_gesture, "pressed",
                     G_CALLBACK (tap_action), swatch);
 
+  swatch->priv->key_controller = gtk_event_controller_key_new (GTK_WIDGET (swatch));
+  g_signal_connect (swatch->priv->key_controller, "key-pressed",
+                    G_CALLBACK (key_controller_key_pressed), swatch);
+
   gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (swatch)), "activatable");
 
   swatch->priv->overlay_widget = g_object_new (GTK_TYPE_IMAGE,